﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FTPLib;
using System.Timers;


namespace Awesome_FTPlib
{
    public static class FTPClient
    {
        public static string Server { get; set; }
        public static string Username { get; set; }
        public static string Password { get; set; }

        public static bool Connected { get; set; }
        public static string CurrentPath 
        {
            get 
            { 
                return _CurrentPath;
            }
            set
            {
                try
                {
                    if (Connected)
                    {

                        TheFTP.ChangeDir(value, _CurrentPath);
                        _CurrentPath = value;

                    }
                    else
                    {
                        throw new Exception("Not Connected");

                    }

                }
                catch (Exception EX)
                {
                    throw EX;

                }


            }
        
        }
        private static string _CurrentPath;
        private static FTP TheFTP;
        private static Timer KeepALiveTimer;
        

        public static void Connect()
        {
            try
            {
                
                Connected = true;
                TheFTP = new FTP(Server, Username, Password, ".");
                TheFTP.Connect(Server, Username, Password);
                KeepALiveTimer = new Timer(300 * 1000);
                KeepALiveTimer.Elapsed += new ElapsedEventHandler(KeepALiveTimer_Elapsed);
                Console.WriteLine("Connected");
                TheFTP.ChangeDir("public_html");
                _CurrentPath = "public_html";
            }
            catch (Exception EX)
            {
                Connected = false;
                throw EX;
            }
        }

        public static void Disconnect()
        {
            if (!Connected)
                throw new Exception("The CLient was not connected therfore you cant disconnect it.");

            TheFTP.Disconnect();
            KeepALiveTimer.Stop();



        }

        public static  UploadWorker UploadFile(string localpath, string serverpath)
        {
            UploadWorker UW = new UploadWorker(TheFTP, localpath, serverpath);

            return UW;

        }

        static void  KeepALiveTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (!Connected)
                throw new Exception("The CLient was not connected therfore you can't send commands.");
            TheFTP.SendCommand("NOOP");
            KeepALiveTimer.Start();
        }


        
    }
}
